package com.xinxin.leetcode.problem463;

/**
 * @author ShiXinXin
 * @date 2020-01-04 20:19
 */
public class Solution {
    public int islandPerimeter(int[][] grid) {
        int length = 0;
        int height = grid.length;
        for (int i = 0; i < height; i++) {
            int width = grid[i].length;
            for (int j = 0; j < width; j++) {
                if (grid[i][j] == 0) {
                    if (i != height - 1 && grid[i + 1][j] == 1) {
                        length++;
                    }
                    if (j != width - 1 && grid[i][j + 1] == 1) {
                        length++;
                    }
                } else {
                    if (i == 0) {
                        length++;
                    }
                    if (i == height - 1) {
                        length++;
                    }
                    if (i != height - 1 && grid[i + 1][j] == 0) {
                        length++;
                    }
                    if (j == 0) {
                        length++;
                    }
                    if (j == width - 1) {
                        length++;
                    }
                    if (j != width - 1 && grid[i][j + 1] == 0) {
                        length++;
                    }
                }
            }
        }
        return length;
    }

    public static void main(String[] args) {
        int[][] arr = {{0, 1}};
        System.out.println(new Solution().islandPerimeter(arr));
    }
}
